package bma.common.thrift;

import org.apache.thrift.TApplicationException;
import org.apache.thrift.TException;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TMessage;
import org.apache.thrift.protocol.TProtocol;

public abstract class TProcessorDispatchAbstract extends TProcessor4Error
		implements TProcessorDispatch {

	public TProcessorDispatchAbstract() {
		super();
	}

	@Override
	public boolean process(TProtocol in, TProtocol out) throws TException {
		TProtocolCacheMessage inw = wrapIn(in);
		TMessage msg = inw.readMessageBegin();
		try {
			TProcessor p = dispatch(msg, inw, out);
			if (p == null) {
				super.writeException(new TApplicationException("'" + msg.name
						+ "' miss processor"), msg, out);
				return false;
			}
			return this.process(p, inw, out);
		} catch (TApplicationException e) {
			this.writeException(e, msg, out);
			return false;
		} catch (Exception e) {
			this.handleException(e, msg, out);
			return false;
		}
	}

}
